-- Remove a part of string (specified by start position and length)

function remove (orig_str, start_pos, len)
	if len < 0 then
		return nil
	elseif len == 0 then
		return orig_str
	end

	if start_pos == 1 then
		return string.sub(orig_str, start_pos + len, -1)
	elseif start_pos == -1 then
		return string.sub(orig_str, 1, -2)
	else
		first_part = string.sub(orig_str, 1, start_pos - 1)
		second_part = string.sub(orig_str, start_pos, -1)
		if len > #second_part or len == #second_part then
			return first_part
		else
			second_part = string.sub(orig_str, start_pos + len, -1)
			return (first_part .. second_part)
		end
	end
end

-- test
print(remove("hello world", 7, 4))
print(remove("hello world", 2, 4))
